/*
 *      Div.java
 *
 *      Copyright 2008 Pablo Nicolas Diaz Bilotto <pablonicolas.diaz@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
package ilanguage.iltokens.operations.arimethic;

import i386CodeGenerator.RegisterAssigner;
import ilanguage.iltokens.IL_SSA;
import ilanguage.iltokens.operations.Operations;
import ilanguage.iltokens.operations.TypeSafeTwoOperandIL_SSA;
import lexer.EntryType;

/**
 * The Class Div.
 */
public class Div extends TypeSafeTwoOperandIL_SSA {

	/**
	 * Instantiates a new div.
	 *
	 * @param firstToken
	 *            the first token
	 * @param secToken
	 *            the sec token
	 */
	public Div(IL_SSA firstToken, IL_SSA secToken) {
		super(firstToken, secToken);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see ilanguage.iltokens.SequenciableToken#GenerateCodeMachine()
	 */
	public String GenerateCode() {
		IL_SSA firstArg = this.getFirstArg();
		IL_SSA secondArg = this.getSecondArg();
		String codigo = "\t; Comienzo de Division\n";

		if (this.solveTypes() == EntryType.INT)
			codigo += divINTGenerateCode(firstArg.getResultRegister(),
					secondArg.getResultRegister());
		else
			codigo += divDOUBLEGenerateCode(firstArg, secondArg);

		codigo += "\t; Fin de Division\n\n";
		return codigo;
	}

	private String divDOUBLEGenerateCode(IL_SSA firstArg, IL_SSA secondArg) {
		String codigo = "";
		String first = firstArg.getResultRegister();
		String second = secondArg.getResultRegister();



		if ((first != "FPU") && (second != "FPU")) {

			if (first.startsWith("_"))
				codigo += "\t\tfld " + first + "\n";

			if (second.startsWith("_"))
				codigo += "\t\tfld " + second + "\n";

			codigo += "\t\tftst\n";
			codigo += "\t\tfstsw FlagVar\n";
			codigo += "\t\tmov ax, FlagVar\n";
			codigo += "\t\tsahf \n";
			codigo += "\t\tcall zerodiv\n";
		} else if ((first != "FPU") && (second == "FPU")) {
			codigo += "\t\tfld " + first + "\n";

			codigo += "\t\tfxch\n";
			codigo += "\t\tftst\n";
			codigo += "\t\tfstsw FlagVar\n";
			codigo += "\t\tmov ax, FlagVar\n";
			codigo += "\t\tsahf \n";
			codigo += "\t\tcall zerodiv\n";

		} else if ((first == "FPU") && (second != "FPU")) {
			codigo += "\t\tfld " + second + "\n";

			codigo += "\t\tftst\n";
			codigo += "\t\tfstsw FlagVar\n";
			codigo += "\t\tmov ax, FlagVar\n";
			codigo += "\t\tsahf \n";
			codigo += "\t\tcall zerodiv\n";
		} else if ((first == "FPU") && (second == "FPU")) {
			codigo += "\t\tftst\n";
			codigo += "\t\tfstsw FlagVar\n";
			codigo += "\t\tmov ax, FlagVar\n";
			codigo += "\t\tsahf \n";
			codigo += "\t\tcall zerodiv\n";
		}
		codigo += "\t\tfdiv\n";
		this.setResultRegister("FPU");
		return codigo ;
	}



	private String divINTGenerateCode(String firstArg, String secondArg) {
		String codigo = "";
		String regAux = "";
		codigo += "\t\t mov DX , 0\n";
		codigo += "\t\t mov AX , " + firstArg + "\n";
		RegisterAssigner.getInstance().liberarRegistro(firstArg);

		if (!RegisterAssigner.getInstance().esRegistro(secondArg)) {
			RegisterAssigner.getInstance().liberarRegistro(secondArg);
			regAux = RegisterAssigner.getInstance().getRegistro();
			codigo += "\t\t mov " + regAux + " , " + secondArg + "\n";
		} else
			regAux = secondArg;

		codigo += "\t\t cmp " + regAux + ",0\n";
		codigo += "\t\t call zerodiv\n";
		codigo += "\t\t idiv " + regAux + "\n";
		codigo += "\t\t mov " + regAux + " , AX \n";


		this.setResultRegister(regAux);
		return codigo;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see ilanguage.iltokens.operations.OperationToken#getOperationName()
	 */
	@Override
	public String getOperationName() {
		return Operations.DIV.toString();
	}

}
